#include <REGX52.H>
#include "Delay.h"
#include "LCD1602.h"
#include "Timer0.h"
#include "KEY.h"

unsigned char Sec,Min,Hour;
unsigned char Mode,KeyNum=0;



void StopWatch_Init()//秒表初始化，全部清零
{
	LCD_ShowString(1,1,"Clock:");	//上电显示静态字符串
	LCD_ShowString(2,1,"00:00:00");		//清零
	Sec=0;Min=0;Hour=0;
	Mode=0;
	
}

void main()
{
	LCD_Init();
	Timer0Init();
	StopWatch_Init();
	
	while(1)
	{
		KeyNum=Key();
		if(KeyNum==1)//按键1开启模式1 开始计时
		{
			Mode=1;
		}
		else if(KeyNum==2) //按键2 停止计时
		{
			Mode=0;
//			LCD_ShowNum(2,1,Hour,2);	//显示时分秒
//			LCD_ShowNum(2,4,Min,2);
//			LCD_ShowNum(2,7,Sec,2);
		}	
		else if(KeyNum==3) //按键3 清零
		{
			StopWatch_Init();
		}	
		LCD_ShowNum(2,1,Hour,2);	//显示时分秒
		LCD_ShowNum(2,4,Min,2);
		LCD_ShowNum(2,7,Sec,2);
	}
}

void Timer0_Routine() interrupt 1
{
	static unsigned int T0Count;
	TL0 = 0x66;		//设置定时初值
	TH0 = 0xFC;		//设置定时初值
	if(Mode==1)//模式1 开始计时
	{
		T0Count++;
		if(T0Count>=1000)	//定时器分频，1s
		{
			T0Count=0;
			Sec++;			//1秒到，Sec自增
			if(Sec>=60)
			{
				Sec=0;		//60秒到，Sec清0，Min自增
				Min++;
				if(Min>=60)
				{
					Min=0;	//60分钟到，Min清0，Hour自增
					Hour++;
					if(Hour>=24)
					{
						Hour=0;	//24小时到，Hour清0
					}
				}
			}
		}
	}
}
